package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        // 查询计划表
        NursingPlan nursingPlan = this.getById(id);
       BeanUtil.copyProperties(nursingPlan,nursingPlanVo);

        //查询关联表
        List<NursingProjectPlanVo> nursingProjectPlanVoList = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(nursingProjectPlanVoList);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional // 多个sql 一起成功要么一起失败
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1.插入计划表
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //插入计划表，给我返回一个主键值--->id
        this.save(nursingPlan);

        //2.插入计划-项目表
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        for (NursingProjectPlan projectPlan : projectPlans) {
            projectPlan.setPlanId(nursingPlan.getId());
        }

        return nursingProjectPlanMapper.batchSave(projectPlans);
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1.先删计划-项目表
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(nursingPlanDto.getId());
        //2.插入计划-项目表
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        projectPlans.forEach(projectPlan->{
            projectPlan.setPlanId(nursingPlanDto.getId());
        });
        nursingProjectPlanMapper.batchSave(projectPlans);

        //3.更新计划表
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        return updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanById(Long id)
    {
        //1.删计划-项目表
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(id);
        //2.删计划表
        return this.removeById(id) ? 1 : 0;
    }
}
